package chen.web.config.spring;

import java.sql.DriverManager;
import java.sql.SQLException;

import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;

import chen.web.InitMyContextFilter;
import chen.web.MyFilter;
import chen.web.config.ConfigRepository;
import chen.web.config.SystemConfig;
import chen.web.event.EventSupport;
import chen.web.firewall.AccountFilter;
import chen.web.firewall.AccountTable;
import chen.web.firewall.IpFilter;
import chen.web.firewall.IpTable;
import chen.web.firewall.UuidFilter;
import chen.web.firewall.UuidTable;
import chen.web.forum.CategoryDynamicInfoListeners;
import chen.web.forum.CategoryManager;
import chen.web.forum.ForumAccountListeners;
import chen.web.forum.ForumAccountLoader;
import chen.web.forum.PostCountResetTask;
import chen.web.forum.TopicInfoListeners;
import chen.web.forum.TopicManager;
import chen.web.user.RecordOnlineSecondsListeners;
import chen.web.user.SessionSupport;
import chen.web.util.jdbc.wrapper.MyDriver;

/**
 * 由于此类的初始化会和其它类会相互引用，故此类的初被化放在程序加载后
 * @author chen
 *
 */
public class InitAndDestroy implements ApplicationListener<org.springframework.context.ApplicationEvent> {
	private @Autowired EventSupport es;
	private @Autowired SessionSupport ss;

	@Override
	public void onApplicationEvent(ApplicationEvent event) {
		// TODO Auto-generated method stub
		if(event instanceof ContextClosedEvent){
			destroy((ContextClosedEvent)event);
		} else if(event instanceof ContextRefreshedEvent){
			init((ContextRefreshedEvent)event);
		}
	}
	
	private PostCountResetTask postCountResetTask = null;
	private void init(ContextRefreshedEvent event) {
		// TODO Auto-generated method stub
		initEventSupport();
		initFilter();
	}
	
	
	private @Autowired MyDriver driver;
	private void destroy(ContextClosedEvent event){
		postCountResetTask.stop();
		try {
			DriverManager.deregisterDriver(driver);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException("注销数据库驱动失败：", e);
		}
		//关闭缓存
		CacheManager m = CacheManager.getInstance();
		if(m != null) m.shutdown();
	}


	private void initEventSupport(){
		es.addListener(new RecordOnlineSecondsListeners.MyOnlineListener());
		es.addListener(new RecordOnlineSecondsListeners.MyOfflineListener(ConfigRepository.instance().get(SystemConfig.class).getSessionTimeOut()));
	}

	private @Autowired IpTable ipTable;
	private @Autowired UuidTable uuidTable;
	private @Autowired AccountTable accountTable;
	private void initFilter(){
		IpFilter ipFilter = MyFilter.getFilter(IpFilter.class);
		if(ipFilter != null){
			ipFilter.setTable(ipTable);	
		}	
		
		UuidFilter uuidFilter = MyFilter.getFilter(UuidFilter.class);
		if(uuidFilter != null){
			uuidFilter.setTable(uuidTable);	
		}
		
		AccountFilter accountFilter = MyFilter.getFilter(AccountFilter.class);
		if(accountFilter != null){
			accountFilter.setTable(accountTable);	
		}	
		
		InitMyContextFilter contextFilter = MyFilter.getFilter(InitMyContextFilter.class);
		if(contextFilter !=null){
			contextFilter.setSessionSupport(ss);
		}
	}
}
